package at.fhj.swd10.eshop.database;

import at.fhj.swd10.eshop.domain.User;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 * @author David
 */
public class UserDao implements Serializable {

    private static final UserDao userDao = new UserDao();

    private UserDao() {
    }

    public static UserDao getUserDao() {
        return userDao;
    }

    public void insertUser(User user) throws Exception {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();

        try {
            session.save(user);
            transaction.commit();
        } catch (Exception ex) {
            transaction.rollback();
            throw ex;
        } finally {
            session.close();
        }
    }

    public void updateUser(User user) throws Exception {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();

        try {
            session.merge(user);
            transaction.commit();
        } catch (Exception ex) {
            transaction.rollback();
            throw ex;
        } finally {
            session.close();
        }
    }

    public void deleteUser(User user) throws Exception {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();

        try {
            session.delete(session.merge(user));
            transaction.commit();
        } catch (Exception ex) {
            transaction.rollback();
            throw ex;
        } finally {
            session.close();
        }
    }

    public List<User> getUserList() throws Exception {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();

        try {
            Query query = session.createQuery("from User");
            List<User> userList = query.list();
            return userList;
        } catch (Exception ex) {
            transaction.rollback();
            throw ex;
        } finally {
            session.close();
        }
    }

    public User getUserById(int id) throws Exception {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();

        try {
            User user = (User) session.get(User.class, id);
            return user;
        } catch (Exception ex) {
            transaction.rollback();
            throw ex;
        } finally {
            session.close();
        }
    }

    public User getUserByUsername(String username) throws Exception {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();

        try {
            Query query = session.createQuery("from User where username = :username");
            query.setParameter("username", username);
            List<User> userList = query.list();
            if (userList.isEmpty()) {
                return null;
            }
            return userList.get(0);
        } catch (Exception ex) {
            transaction.rollback();
            throw ex;
        } finally {
            session.close();
        }
    }

    public User getUserByUsernameAndPassword(String username, String password) throws Exception {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();

        try {
            Query query = session.createQuery("from User where username = :username and password = :password");
            query.setParameter("username", username);
            query.setParameter("password", password);
            List<User> userList = query.list();
            if (userList.isEmpty()) {
                return null;
            }
            return userList.get(0);
        } catch (Exception ex) {
            transaction.rollback();
            throw ex;
        } finally {
            session.close();
        }
    }
}
